package cn.harry.modular.news.controller;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.R;
import cn.harry.common.enums.BusinessType;
import cn.harry.modular.news.service.CategoryService;
import cn.harry.modular.news.domain.Category;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * category前端控制层
 *
 * @author harry
 * @公众号 Harry技术
 */
@Tag(name = "category接口")
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
public class CategoryController  {

    private final CategoryService categoryService;

    @Operation(summary = "列表")
    @GetMapping(value = "/list")
    public R<List<Category>> list(@ParameterObject  Category entity) {
        return R.success(categoryService.list(Wrappers.lambdaQuery(entity)));
    }
    /**
     * 分页查询列表
     */
    @Operation(summary = "分页查询")
    @GetMapping(value = "/page")
    public R<IPage<Category>> page(@ParameterObject Page<Category> page,@ParameterObject  Category entity) {
        return R.success(categoryService.page(page, Wrappers.lambdaQuery(entity)));
    }

    /**
     * 根据id获取详情
     */
    @Operation(summary = "根据id获取详情")
    @GetMapping(value = "/{id}")
    public R<Category> getById(@PathVariable Long id) {
        return R.success(categoryService.getById(id));
    }

    /**
     * 新增
     */
    @Operation(summary = "新增")
    @SysLog(title = "t_category", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Integer> save(@RequestBody Category entity) {
        return categoryService.save(entity) ? R.success() : R.failed();
    }

    /**
     * 更新
     */
    @Operation(summary = "更新")
    @SysLog(title = "t_category", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Integer> update(@RequestBody Category entity) {
        return categoryService.updateById(entity) ? R.success() : R.failed();
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @SysLog(title = "t_category", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}")
    public R<Integer> deleteByIds(@Parameter(description = "多个以英文逗号(,)拼接") @PathVariable Long[] ids) {
        return categoryService.removeBatchByIds(Arrays.asList(ids)) ? R.success() : R.failed();
    }
}
